<?php

namespace app\api\controller;


use app\admin\model\Man;
use app\admin\model\Order;
use app\admin\model\Ordermoney;
use app\common\controller\Api;

class Repaire extends Api
{
    
    protected $noNeedLogin =[];
    protected $noNeedRight = '*';
    /**
     * 申请成为师傅
     */
    public function addToRepair()
    {
        $idImage = $this->request->post('idImage','');
        $jsImages = $this->request->post('jsImages','');
        $phone = $this->request->post('phone');
        $typeId = $this->request->post('typeId');
        $password = $this->request->post('password','');
        if(empty($password)) {
            $this->error('fail');
        }
        $salt = \fast\Random::alnum();
        $password  = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
        
        Man::insert(['salt' => $salt,'user_id' => $this->auth->id,'id_image' =>   $idImage,'js_images' =>  $jsImages,'phone' => $phone,'type_id' => $typeId,'password' => ]);
    }
    
    /**
     * 我的订单
     * @param mixed $page
     * @return void
     */
    public function order($page = 1)
    {
        $list = Order::where(['repair_user_id' => $this->auth->id])->paginate(20,false,['page' => $page]);
        $this->success('suc',$list);
    }
    /**
     * 修改为待上门
     */
    public function changeOrder($id)
    {
        Order::where(['id' => $id])->update(['status' => 1]);
        $this->success('suc');
    }
    /**
     * 诊断并生成报价
     */
    public function diagnosis($id = '')
    {
        $result = $this->request->post('result','');//检测结果
        $detail = $this->request->post('detail','');//详情
        $machine = $this->request->post('machine');//设备型号
        $money = 10;
       
        Order::where(['id' => $id])->update(['result' => $result,'detail' => $detail,'machine' => $machine,'money' => $money]);
        OrderMoney::insert(['order_id' => $id,'money' =>$money,'type' => 0,'time' => time() ]);
        $this->success('suc',['money' => $money]);
    }
    /**
     * 修改订单金额
     * 订单也要修改成待确认
     */
    public function changeMoney($id = '',$money = '')
    {
        Order::where(['id' => $id])->update(['money' => $money,'status' => 7]);
        OrderMoney::insert(['order_id' => $id,'money' =>$money,'type' => 1,'time' => time(),'user_id'=> $this->auth->id ]);
    }
    /**
     * 完工确认
     */
    public function confirm($id,$images)
    {
        Order::where(['id' => $id])->update(['status' => 10,'images' => $images]);
        $this->success('suc');
    }
    public function getUserInfo()
    {
        $detail = Man::where(['user_id' => $this->auth->id])->find();
        $this->success('suc',$detail);
    }
   
}
   

   
